<?php
namespace app\madmin\controller;
use app\madmin\model\WechatMenusModel;

/**
 * 微信菜单控制器
 * @author  黄明杰 vx:HMJ125855
 * @version 20190201
 */
class WechatMenus extends PublicBase{

	# 微信菜单列表方法
    public function showList(){
        $WechatMenus = new WechatMenusModel;

        $Lists = $WechatMenus->mList();

        # 模版赋值
        return $this->fetch('WechatMenus/showList', [
            'Lists' => $WechatMenus::mTree($Lists)
        ]);
    }

    # 微信菜单新增方法
    public function showAdd(){
        $WechatMenus = new WechatMenusModel;

        if(request()->isPost()){
            $Param = input('post.');

            # 查询一级菜单个数(不能大于3个)
            $CountA = $WechatMenus->mCount(['menu_pid'=>0]);
            if($CountA >= 3){
                $this->ShowData(2, lang('WxCdSx1'));
            }

            # 查询二级菜单个数(不能大于5个)
            $CountB = $WechatMenus->mCount(['menu_pid'=>$Param['menu_pid']]);
            if($CountB >= 5){
                $this->ShowData(2, lang('WxCdSx2'));
            }

            $Back = $WechatMenus->mAdd($Param);

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }else{
            # 模版赋值
            return $this->fetch('WechatMenus/showAdd', [
                'Info'  => Null,
                'Menu'  => $this->showMenu(),
                'Type'  => $this->getLexiconCache('WECHAT_MENU_TYPE')
            ]);
        }
    }

    # 微信菜单更新方法
    public function showEdit(){
        $WechatMenus = new WechatMenusModel;

    	$ID = input('param.id');

        if(request()->isPost()){
            $Param = input('post.');

            # 查询一级菜单个数(不能大于3个)
            $CountA = $WechatMenus->mCount(['menu_pid'=>0]);
            if($CountA >= 3){
                $this->ShowData(2, lang('WxCdSx1'));
            }

            # 查询二级菜单个数(不能大于5个)
            $CountB = $WechatMenus->mCount(['menu_pid'=>$Param['menu_pid']]);
            if($CountB >= 5){
                $this->ShowData(2, lang('WxCdSx2'));
            }

            $Back = $WechatMenus->mEdit(['menu_id'=>$ID], $Param);

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }else{
            $Info = $WechatMenus->mInfo(['menu_id'=>$ID]);

            if(empty($Info)){
                $this->ShowData(2, lang('XtCwCxCw'));
            }else{
                # 模版赋值
                return $this->fetch('WechatMenus/showAdd', [
                    'Info'  => $Info,
                    'Menu'  => $this->showMenu(),
                    'Type'  => $this->getLexiconCache('WECHAT_MENU_TYPE')
                ]);
            }
        }
    }

    # 微信菜单删除方法
    public function showDel(){
        if(request()->isPost()){
            $WechatMenus = new WechatMenusModel;

            $ID = input('post.id');

            $Back = $WechatMenus->mDel(['menu_id'=>$ID]);

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }
    }

    # 微信菜单生成方法
    public function showMake(){
        if(request()->isPost()){
            $Access = $this->WechatAccessTokenCache();

            $WechatMenus = new WechatMenusModel;

            $Back = $WechatMenus->mMake($Access, $this->showGain());

            $this->ShowData($Back['code'], $Back['msgs']);
        }
    }

    # 微信菜单删除方法
    public function showDele(){
        if(request()->isPost()){
            $Access = $this->WechatAccessTokenCache();

            $WechatMenus = new WechatMenusModel;

            $Back = $WechatMenus->mDele($Access);

            $this->ShowData($Back['code'], $Back['msgs']);
        }
    }

    # 微信菜单下拉方法
    private function showMenu(){
        $WechatMenus = new WechatMenusModel;

        $Where = [];
        $Where['menu_pid'] = 0;

        return $WechatMenus->mList($Where, 'menu_id,menu_title');
    }

    # 微信菜单获取方法
    private function showGain(){
        $WechatMenus = new WechatMenusModel;

        $Where = [];
        $Where['menu_state'] = 1;
        $Lists = $WechatMenus->mList($Where, '*', 'menu_sort DESC');

        $Lists = $WechatMenus::mMenu($Lists);

        $Array = [];

        foreach($Lists as $k => $v){
            switch($v['menu_type']){
                case '1':
                    $Array[$k]['name'] = $v['menu_title'];
                    if(!empty($v['menu_child'])){
                        foreach($v['menu_child'] as $s => $p){
                            switch($p['menu_type']){
                                case '2':
                                    $Array[$k]['sub_button'][$s]['type'] = 'view';
                                    $Array[$k]['sub_button'][$s]['name'] = $p['menu_title'];
                                    $Array[$k]['sub_button'][$s]['url'] = $p['menu_link'];
                                    break;
                                case '3':
                                    $Array[$k]['sub_button'][$s]['type'] = 'click';
                                    $Array[$k]['sub_button'][$s]['name'] = $p['menu_title'];
                                    $Array[$k]['sub_button'][$s]['key'] = $p['menu_key'];
                                    break;
                                case '4':
                                    $Array[$k]['sub_button'][$s]['type'] = 'miniprogram';
                                    $Array[$k]['sub_button'][$s]['name'] = $p['menu_title'];
                                    $Array[$k]['sub_button'][$s]['url'] = $p['menu_link'];
                                    $Array[$k]['sub_button'][$s]['appid'] = $p['menu_appid'];
                                    $Array[$k]['sub_button'][$s]['pagepath'] = $p['menu_paths'];
                                    break;
                                default:
                                    break;
                            }
                        }
                    }else{
                        $Array[$k]['sub_button'] = [];
                    }
                    break;
                case '2':
                    $Array[$k]['type'] = 'view';
                    $Array[$k]['name'] = $v['menu_title'];
                    $Array[$k]['url'] = $v['menu_link'];
                    break;
                case '3':
                    $Array[$k]['type'] = 'click';
                    $Array[$k]['name'] = $v['menu_title'];
                    $Array[$k]['key'] = $v['menu_key'];
                    break;
                case '4':
                    $Array[$k]['type'] = 'miniprogram';
                    $Array[$k]['name'] = $v['menu_title'];
                    $Array[$k]['url'] = $v['menu_link'];
                    $Array[$k]['appid'] = $v['menu_appid'];
                    $Array[$k]['pagepath'] = $v['menu_paths'];
                    break;
                default:
                    break;
            }
        }

        return $Array;
    }

}